package com.library.relation.service;

import com.library.entity.Book;
import com.library.entity.BookRelation;
import com.library.entity.Staff;
import com.library.relation.dao.BookRelationDaoImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional(readOnly=false)
public class BookRelationServiceImpl {
	
	@Resource
	private BookRelationDaoImpl bookRelationDao;

	public void addRelation(BookRelation relation){
		this.bookRelationDao.saveRelation(relation);
	}

    @Transactional(readOnly=true)
	public BookRelation getRelationById(Integer relationId)
	{
		return this.bookRelationDao.getRelationById(relationId);
	}

    @Transactional(readOnly=true)
	public List<BookRelation> getBookRelation(Book book){
		return this.bookRelationDao.findByBook(book);
	}

	@Transactional(readOnly=true)
	public BookRelation getBookRelation(Book b1,Book b2){
		return this.bookRelationDao.findByBookAndBook(b1,b2);
	}

	public void updateRelation(BookRelation relation){
		this.bookRelationDao.updateRelation(relation);
	}

    @Transactional(readOnly=true)
	/*查找全部评价记录*/
	public List<BookRelation> getAllRelation()
	{
		return this.bookRelationDao.getAll();
	}

	/*删除指定关系记录*/
	public void deleteRelation(int relationId){
		BookRelation relation = this.getRelationById(relationId);
		this.bookRelationDao.deleteRelation(relation);
    }

    //删除book相关的relation
    public void deleteRelationByBook(Book book){
		List<BookRelation> bookRelations = this.getBookRelation(book);
		Iterator<BookRelation> iterator = bookRelations.iterator();
		BookRelation bookRelation = null;

		while(iterator.hasNext()){
			bookRelation = iterator.next();
			this.bookRelationDao.deleteRelation(bookRelation);
		}
	}

	//删除Set<book>相关的relation
	public void deleteRelationByBook(Set<Book> bookSet){
        Iterator<Book> bookIterator = bookSet.iterator();
        Book book = null;

		while(bookIterator.hasNext()){
            book = bookIterator.next();
            this.deleteRelationByBook(book);
		}
	}

    //基于员工借阅情况更新图书之间的联系次数
    public void updateAllRelationByStaffs(Staff staff, List<Staff> staffList){
		BookRelation relation = null;
		Set<Book> bookSetOne;
		Set<Book> bookSetTwo;
		Book b1 = null;
		Book b2 = null;
		Iterator<Book> itOne = null;
		Iterator<Book> itTwo = null;

		for(int i = 0;i < staffList.size();++i){
			bookSetOne = staffList.get(i).getBooks();
			bookSetTwo = staff.getBooks();

			itOne = bookSetOne.iterator();
			while(itOne.hasNext()){
				b1 = itOne.next();
				itTwo = bookSetTwo.iterator();
				while (itTwo.hasNext()){
					b2 = itTwo.next();

					if(b1.equals(b2)){		//同一图书 继续循环
						continue;
					}else{
						relation = this.getBookRelation(b1,b2);
						if(relation != null){
							relation.setBookOne(b1);
							relation.setBookTwo(b2);
							relation.setCount(relation.getCount() + 1);

							this.bookRelationDao.updateRelation(relation);
						}else{
							relation = new BookRelation();
							relation.setBookOne(b1);
							relation.setBookTwo(b2);
							relation.setCount(1);

							this.bookRelationDao.saveRelation(relation);
						}
					}
				}

			}	//while
		}
	}

	//基于目标员工的图书与其他图书联系次数返回最相似的6图书 降序
	public Set<Book> getRecommendationBookByStaff(Staff staff){
		Set<Book> bookSet = staff.getBooks();		//员工有过行为的图书
		List<BookRelation> relations = null;
		Iterator<Book> iterator = bookSet.iterator();
		Book book = null;
		Set<Book> books = new HashSet<Book>();		//推荐的图书

		while(iterator.hasNext()){
			book = iterator.next();
			relations = this.bookRelationDao.findByBookOrderByCountDesc(book);

			if(relations != null){
				if (books.size() > 5) break;
				for(int i = 0;i < relations.size() && books.size() <= 5;++i){
					if(bookSet.contains(relations.get(i).getBookTwo())){		//员工已经看过
						continue;
					}
					books.add(relations.get(i).getBookTwo());		//添加到推荐列表
				}
			}
		}
		return books;
	}

	//基于目标员工的图书与其他图书联系次数返回最相似的10图书关系 降序
	public Set<BookRelation> getRecommendationBookRelationByStaff(Staff staff){
		Set<Book> bookSet = staff.getBooks();		//员工有过行为的图书
		List<BookRelation> relations = null;
		Iterator<Book> iterator = bookSet.iterator();
		Book book = null;
		Set<BookRelation> bookRelations = new HashSet<BookRelation>();		//推荐的图书关系


		while(iterator.hasNext()){
			book = iterator.next();
			relations = this.bookRelationDao.findByBookOrderByCountDesc(book);

			if(relations != null){
				if(bookRelations.size() >= 10) break;
				for(int i = 0;i < relations.size() && bookRelations.size() <= 10;++i){
					bookRelations.add(relations.get(i));
				}
			}
		}

		return bookRelations;
	}
}
